﻿using Bussiness.Company;
using Bussiness.HomeLoanTrust;
using Bussiness.Language;
using Bussiness.LongTermLoan;
using Bussiness.Product;
using Bussiness.ProductFAQ;
using FOModels.Company;
using FOModels.HomeLoanTrust;
using FOModels.Language;
using FOModels.LongTermLoan;
using FOModels.Product;
using FOModels.ProductFAQ;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using FOSystem.CultureHelper;
using Bussiness.Status;
using FOModels.Status;
using System.Configuration;
using System.Net.Mail;
namespace FO.Controllers
{
    public class LongTermLoanController : CultureController
    {
        //
        // GET: /LongTermLoan/

        public ActionResult Index()
        {
            HomeLoanTrustBLL homeloanbll = new HomeLoanTrustBLL();
            LongTermLoanBLL bll = new LongTermLoanBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            ProductBLL probll = new ProductBLL();
            int LongTermLoanID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["LongTermLoanID"].ToString());
            ViewBag.HomeLoanPlan = homeloanbll.getHomeLoanPlan(lang.LangID);
            ViewBag.LongTermLoan = bll.getLongTermLoanDefault(lang.LangID);
            ViewBag.ProDes = probll.getProductByID(LongTermLoanID, lang.LangID);
            return View();
        }
        public ActionResult Detail(int LongTermLoanID)
        {
            int LongTermLoanIDs = int.Parse(System.Configuration.ConfigurationManager.AppSettings["LongTermLoanID"].ToString());
            LongTermLoanBLL termbll = new LongTermLoanBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            ProductFAQBLL faqbll = new ProductFAQBLL();
            LongTermLoanModels term = new LongTermLoanModels();
            CompanyBLL combll = new CompanyBLL();
            term = termbll.getLongTermLoanByID(LongTermLoanID, lang.LangID);
            ViewBag.FAQ = faqbll.getfaqbyproid(LongTermLoanIDs, lang.LangID);
            ViewBag.TermInterest = combll.getTableLongTermLoanInterestbyComID(term.CompanyID);
            return View(term);
        }
        [HttpPost]
        public ActionResult getLongTermLoanByProvider(int planid, string balance, int years, int CompanyID)
        {
            LongTermLoanBLL termbll = new LongTermLoanBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<LongTermLoanModels> list = new List<LongTermLoanModels>();
            list = termbll.getLongTermLoanByProvider(planid, balance, years, CompanyID, lang.LangID);
            return Json(list);
        }

        [HttpPost]
        public ActionResult getLongTermLoan(int planid, string balance, int years)
        {
            LongTermLoanBLL termbll = new LongTermLoanBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<LongTermLoanModels> list = new List<LongTermLoanModels>();
            list = termbll.getLongTermLoan(planid, balance, years, lang.LangID);
            return Json(list);
        }
        public ActionResult LongTermLoanInfo(int LongTermLoanID)
        {
            LongTermLoanBLL termbll = new LongTermLoanBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            int LongTermLoanIDs = int.Parse(System.Configuration.ConfigurationManager.AppSettings["LongTermLoanID"].ToString());
            ProductFAQBLL faqbll = new ProductFAQBLL();
            LongTermLoanModels list = new LongTermLoanModels();
            list = termbll.getLongTermLoanByID(LongTermLoanID, lang.LangID);
            ViewBag.FAQ = faqbll.getfaqbyproid(LongTermLoanIDs, lang.LangID);
            Fcom.IDLib.SaveMoneyID.AccountsynClient client = new Fcom.IDLib.SaveMoneyID.AccountsynClient();
            FOModels.User.User us = null;
            if (User.Identity.Name != "")
            {
                Fcom.IDLib.SaveMoneyID.FullUserInfo fullinfo = client.getUserInfo(User.Identity.Name);
                us = new FOModels.User.User();
                us.DisplayName = fullinfo.Fullname;
                us.Email = fullinfo.Email;
                us.GroupID = fullinfo.GroupID;
                us.Status = fullinfo.Status;
                us.Username = fullinfo.Username;
                us.Phone = fullinfo.Phone;
                us.DOB = fullinfo.DateOfBirth;

            }
            ViewBag.UserInfo = us;

            return View(list);
        }

        public ActionResult TermSavingByProvider(int CompanyID)
        {
            HomeLoanTrustBLL bll = new HomeLoanTrustBLL();
            LongTermLoanBLL termbll = new LongTermLoanBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            ProductBLL probll = new ProductBLL();
            int LongTermLoanID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["LongTermLoanID"].ToString());
            ViewBag.HomeLoanPlan = bll.getHomeLoanPlan(lang.LangID);
            ViewBag.LongTermLoan = termbll.getLongTermLoanDefaultByProvider(CompanyID, lang.LangID);

            return View();

        }
        [Authorize(Roles = "8,9")]
        public ActionResult ShowCase()
        {
            return View();
        }
        [Authorize(Roles = "8,9")]
        public ActionResult FAQShowCase()
        {
            return View();
        }
        [Authorize(Roles = "8,9")]
        public ActionResult ManagerLongTermLoan()
        {
            return View();
        }
        public JsonResult getLongTermLoanJson()
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            LongTermLoanBLL cardbll = new LongTermLoanBLL();
            List<LongTermLoanModels> list = new List<LongTermLoanModels>();
            list = cardbll.getall(lang.LangID);
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult UpdateLongTermLoan(int LongTermLoanID)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            LongTermLoanBLL bll = new LongTermLoanBLL();
            HomeLoanTrustBLL homeloanbll = new HomeLoanTrustBLL();
            List<LongTermLoanModels> cate = new List<LongTermLoanModels>();
            List<HomeLoanPlanModels> plan = new List<HomeLoanPlanModels>();
            CompanyBLL company = new CompanyBLL();
            List<CompanyModels> com = new List<CompanyModels>();
            LongTermLoanModels saving = new LongTermLoanModels();
            plan = homeloanbll.getHomeLoanPlan(lang.LangID);
            com = company.getCompany(lang.LangID);
            saving = bll.getLongTermLoanByIDManager(LongTermLoanID, lang.LangID);

            ViewBag.HomeLoanPlanListID = new SelectList(plan, "HomeLoanPlanID", "HomeLoanPlanName", saving.HomeLoanPlanID);
            ViewBag.CompanyListID = new SelectList(com, "CompanyID", "Name", saving.CompanyID);
            Models.LongTermLoan sa = new Models.LongTermLoan();
            sa.LongTermLoanName = saving.LongTermLoanName;
            sa.CompanyID = saving.CompanyID;
            sa.MinimumBalance = saving.MinimumBalance;
            sa.MaximumBalance = saving.MaximumBalance;
            sa.Description = WebUtility.HtmlDecode(saving.Description);
            sa.Benefit = WebUtility.HtmlDecode(saving.Benefit);
            sa.FeeContent = WebUtility.HtmlDecode(saving.FeeContent);
            sa.MoreInfo = WebUtility.HtmlDecode(saving.MoreInfo);
            sa.Display = saving.Display;
            sa.HomeLoanPlanID = saving.HomeLoanPlanID;
            sa.LongTermLoanID = saving.LongTermLoanID;
            return View(sa);
        }

        [ValidateInput(false)]
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult UpdateLongTermLoan(Models.LongTermLoan de)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            LongTermLoanBLL bll = new LongTermLoanBLL();
            List<HomeLoanCategoryModels> cate = new List<HomeLoanCategoryModels>();
            List<HomeLoanPlanModels> plan = new List<HomeLoanPlanModels>();
            CompanyBLL company = new CompanyBLL();
            List<CompanyModels> com = new List<CompanyModels>();
            LongTermLoanModels saving = new LongTermLoanModels();
            HomeLoanTrustBLL homeloanbll = new HomeLoanTrustBLL();
            plan = homeloanbll.getHomeLoanPlan(lang.LangID);
            com = company.getCompany(lang.LangID);
            saving = bll.getLongTermLoanByIDManager(de.LongTermLoanID, lang.LangID);

            ViewBag.HomeLoanPlanListID = new SelectList(plan, "HomeLoanPlanID", "HomeLoanPlanName", saving.HomeLoanPlanID);
            ViewBag.CompanyListID = new SelectList(com, "CompanyID", "Name", saving.CompanyID);
            if (saving != null)
            {
                saving.LongTermLoanName = de.LongTermLoanName;
                saving.CompanyID = de.CompanyID;
                saving.MinimumBalance = de.MinimumBalance.Replace(".", "").Replace(".", "").Replace(".", "");
                saving.MaximumBalance = de.MaximumBalance.Replace(".", "").Replace(".", "").Replace(".", "");
                saving.Description = WebUtility.HtmlEncode(de.Description);
                saving.Benefit = WebUtility.HtmlEncode(de.Benefit);
                saving.FeeContent = WebUtility.HtmlEncode(de.FeeContent);
                saving.MoreInfo = WebUtility.HtmlEncode(de.MoreInfo);
                saving.Display = de.Display;
                saving.HomeLoanPlanID = de.HomeLoanPlanID;
                bll.UpdateLongTermLoan(saving, lang.LangID);

                return RedirectToAction("ManagerLongTermLoan");
            }
            return View();
        }
        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult InsertLongTermLoan()
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            LongTermLoanBLL bll = new LongTermLoanBLL();
            List<HomeLoanCategoryModels> cate = new List<HomeLoanCategoryModels>();
            CompanyBLL company = new CompanyBLL();
            List<CompanyModels> com = new List<CompanyModels>();
            List<HomeLoanPlanModels> plan = new List<HomeLoanPlanModels>();
            HomeLoanTrustBLL homeloanbll = new HomeLoanTrustBLL();
            plan = homeloanbll.getHomeLoanPlan(lang.LangID);
            com = company.getCompany(lang.LangID);
            ViewBag.HomeLoanPlanListID = new SelectList(plan, "HomeLoanPlanID", "HomeLoanPlanName");
            ViewBag.CompanyListID = new SelectList(com, "CompanyID", "Name");
            return View();

        }

        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult InsertLongTermLoan(FO.Models.LongTermLoanInsert de)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            LongTermLoanBLL bll = new LongTermLoanBLL();
            List<HomeLoanCategoryModels> cate = new List<HomeLoanCategoryModels>();
            CompanyBLL company = new CompanyBLL();
            List<CompanyModels> com = new List<CompanyModels>();
            List<HomeLoanPlanModels> plan = new List<HomeLoanPlanModels>();
            HomeLoanTrustBLL homeloanbll = new HomeLoanTrustBLL();
            plan = homeloanbll.getHomeLoanPlan(lang.LangID);
            com = company.getCompany(lang.LangID);
            ViewBag.HomeLoanPlanListID = new SelectList(plan, "HomeLoanPlanID", "HomeLoanPlanName");
            ViewBag.CompanyListID = new SelectList(com, "CompanyID", "Name");
            if (ModelState.IsValid)
            {
                LongTermLoanModels saving = new LongTermLoanModels();
                LongTermLoanModels savingLang = new LongTermLoanModels();
                saving.LongTermLoanName = de.LongTermLoanName;
                saving.CompanyID = de.CompanyID;
                saving.MinimumBalance = de.MinimumBalance.Replace(".", "").Replace(".", "").Replace(".", "");
                saving.MaximumBalance = de.MaximumBalance.Replace(".", "").Replace(".", "").Replace(".", "");
                saving.Description = WebUtility.HtmlEncode(de.Description);
                saving.Benefit = WebUtility.HtmlEncode(de.Benefit);
                saving.FeeContent = WebUtility.HtmlEncode(de.FeeContent);
                saving.MoreInfo = WebUtility.HtmlEncode(de.MoreInfo);
                saving.HomeLoanPlanID = de.HomeLoanPlanID;
                saving.Display = false;
                bll.InsertLongTermLoan(saving);
                LongTermLoanModels savingItem = bll.getTOP1();
                saving.LongTermLoanID = savingItem.LongTermLoanID;
                savingLang.LongTermLoanID = savingItem.LongTermLoanID;
                savingLang.LongTermLoanName = de.LongTermLoanNameLang;
                savingLang.CompanyID = de.CompanyID;
                savingLang.MinimumBalance = de.MinimumBalance.Replace(".", "").Replace(".", "").Replace(".", "");
                savingLang.MaximumBalance = de.MaximumBalance.Replace(".", "").Replace(".", "").Replace(".", "");
                savingLang.Description = WebUtility.HtmlEncode(de.DescriptionLang);
                savingLang.Benefit = WebUtility.HtmlEncode(de.BenefitLang);
                savingLang.FeeContent = WebUtility.HtmlEncode(de.FeeContentLang);
                savingLang.MoreInfo = WebUtility.HtmlEncode(de.MoreInfoLang);
                savingLang.HomeLoanPlanID = de.HomeLoanPlanID;
                savingLang.Display = false;

                bll.InsertLongTermLoanLang(saving, 1);
                bll.InsertLongTermLoanLang(savingLang, 2);
                return RedirectToAction("ManagerLongTermLoan");
            }
            return View(de);

        }
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public JsonResult DeleteLongTermLoan(int LongTermLoanID)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            LongTermLoanBLL bll = new LongTermLoanBLL();
            LongTermLoanModels del = new LongTermLoanModels();

            del = bll.getLongTermLoanByIDManager(LongTermLoanID, lang.LangID);
            if (del != null)
            {
                bll.DeleteLongTermLoan(del.LongTermLoanID);
                var result = new { Status = "OK" };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            else
            {
                var result = new { Status = "Xóa thất bại" };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
        }


        [Authorize(Roles = "8,9")]
        public ActionResult ManagerFAQ()
        {
            return View();
        }
        public JsonResult getFAQJson()
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            int LongTermLoanID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["LongTermLoanID"].ToString());
            ProductFAQBLL cardbll = new ProductFAQBLL();
            List<ProductFAQModels> list = new List<ProductFAQModels>();
            list = cardbll.getfaqbyproid(LongTermLoanID, lang.LangID);
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult InsertFAQ()
        {
            return View();
        }
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult InsertFAQ(FO.Models.ProductFAQInsert card)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            ProductFAQBLL cardbll = new ProductFAQBLL();
            int LongTermLoanID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["LongTermLoanID"].ToString());
            if (ModelState.IsValid)
            {
                ProductFAQModels items = new ProductFAQModels();
                items.FAQTitles = card.FAQTitles;
                items.FAQAnwers = card.FAQAnwers;
                items.ProductID = LongTermLoanID;
                cardbll.InsertFAQ(items);
                ProductFAQModels faqitems = cardbll.gettop1faq();
                ProductFAQModels itemsLang = new ProductFAQModels();
                items.ProductFAQID = faqitems.ProductFAQID;
                itemsLang.ProductFAQID = faqitems.ProductFAQID;
                cardbll.InsertFAQLang(items, 1);

                itemsLang.FAQTitles = card.FAQTitlesLang;
                itemsLang.FAQAnwers = card.FAQAnwersLang;
                cardbll.InsertFAQLang(itemsLang, 2);
                return RedirectToAction("ManagerFAQ");
            }
            return View(card);
        }

        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult UpdateFAQ(int ProductFAQID)
        {
            ProductFAQBLL cardbll = new ProductFAQBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            ProductFAQModels card = new ProductFAQModels();
            card = cardbll.getfaqbyid(ProductFAQID, lang.LangID);
            Models.ProductFAQ c = new Models.ProductFAQ();
            c.FAQTitles = card.FAQTitles;
            c.FAQAnwers = card.FAQAnwers;
            return View(c);
        }
        [ValidateInput(false)]
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult UpdateFAQ(FO.Models.ProductFAQ card)
        {
            ProductFAQBLL cardbll = new ProductFAQBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            ProductFAQModels ca = new ProductFAQModels();
            ca = cardbll.getfaqbyid(card.ProductFAQID, lang.LangID);
            if (ca != null)
            {
                ca.FAQTitles = card.FAQTitles;
                ca.FAQAnwers = card.FAQAnwers;
                cardbll.UpdateFAQ(ca, lang.LangID);
                return RedirectToAction("ManagerFAQ");
            }
            return View();
        }

        [HttpPost]
        [Authorize(Roles = "8,9")]
        public JsonResult DeleteFAQ(int ProductFAQID)
        {
            ProductFAQBLL bll = new ProductFAQBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            ProductFAQModels card = new ProductFAQModels();
            card = bll.getfaqbyid(ProductFAQID, lang.LangID);
            if (card != null)
            {
                bll.DeleteFAQ(card.ProductFAQID);
                var result = new { Status = "OK" };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            else
            {
                var result = new { Status = "Xóa thất bại" };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
        }

        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult Articles()
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            ProductBLL cardbll = new ProductBLL();
            int LongTermLoanID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["LongTermLoanID"].ToString());
            ProductModels card = new ProductModels();

            card = cardbll.getProductByID(LongTermLoanID, lang.LangID);

            Models.Product c = new Models.Product();
            c.Description = WebUtility.HtmlDecode(card.Description);
            return View(c);
        }
        [ValidateInput(false)]
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult Articles(FO.Models.Product card)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            ProductBLL cardbll = new ProductBLL();
            int LongTermLoanID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["LongTermLoanID"].ToString());
            ProductModels ca = new ProductModels();

            ca = cardbll.getProductByID(LongTermLoanID, lang.LangID);
            if (ca != null)
            {
                ca.Description = WebUtility.HtmlEncode(card.Description);
                cardbll.UpdateProductDesriptionLang(ca, lang.LangID);
                return RedirectToAction("ManagerLongTermLoan");
            }
            return View();
        }

        [HttpPost]
        public JsonResult LongTermLoanApply(int LongTermLoanID, string CustomerName, string CustomerEmail, string CustomerPhone, string CustomerIncome, string CustomerDOB)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            LongTermLoanBLL bll = new LongTermLoanBLL();
            LongTermLoanApplyModels LongTermLoanapp = new LongTermLoanApplyModels();
            LongTermLoanapp.LongTermLoanID = LongTermLoanID;
            LongTermLoanapp.CustomerName = CustomerName;
            LongTermLoanapp.CustomerEmail = CustomerEmail;
            LongTermLoanapp.CustomerPhone = CustomerPhone;
            LongTermLoanapp.CustomerIncome = CustomerIncome;
            LongTermLoanapp.CustomerDOB = CustomerDOB;
            LongTermLoanapp.StatusID = 1;
            int result = bll.LongTermLoanApply(LongTermLoanID, LongTermLoanapp);
            if (result == 1)
            {
                int check = 1;
                return Json(check);
            }
            else
            {
                int check = -1;
                return Json(check);
            }
        }
        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult ContractDetail(int LongTermLoanApplyID)
        {
            LongTermLoanBLL LongTermLoanBLL = new LongTermLoanBLL();
            LongTermLoanApplyModels LongTermLoanapp = new LongTermLoanApplyModels();
            LongTermLoanapp = LongTermLoanBLL.getcontractbyid(LongTermLoanApplyID);
            StatusBLL status = new StatusBLL();
            List<StatusModels> liststatus = new List<StatusModels>();
            liststatus = status.getStatus();
            FO.Models.LongTermLoanApply c = new FO.Models.LongTermLoanApply();
            ViewBag.StatusListID = new SelectList(liststatus, "StatusID", "StatusName", LongTermLoanapp.StatusID);
            c.StatusID = LongTermLoanapp.StatusID;
            c.CustomerName = LongTermLoanapp.CustomerName;
            c.CustomerEmail = LongTermLoanapp.CustomerEmail;
            c.CustomerDOB = LongTermLoanapp.CustomerDOB;
            c.CustomerPhone = LongTermLoanapp.CustomerPhone;
            c.CustomerIncome = LongTermLoanapp.CustomerIncome;
            c.CompanyName = LongTermLoanapp.CompanyName;
            c.StatusName = LongTermLoanapp.StatusName;
            c.LongTermLoanName = LongTermLoanapp.LongTermLoanName;
            c.LongTermLoanID = LongTermLoanapp.LongTermLoanID;
            c.CompanyImg = LongTermLoanapp.CompanyImg;
            c.CompanyName = LongTermLoanapp.CompanyName;
            c.AppDate = LongTermLoanapp.AppDate;
            return View(c);
        }
        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Roles = "8,9")]
        public ActionResult ContractDetail(FO.Models.LongTermLoanApply LongTermLoan)
        {
            LongTermLoanBLL LongTermLoanBLL = new LongTermLoanBLL();
            LongTermLoanApplyModels LongTermLoanapp = new LongTermLoanApplyModels();
            LongTermLoanapp = LongTermLoanBLL.getcontractbyid(LongTermLoan.LongTermLoanApplyID);
            StatusBLL status = new StatusBLL();
            List<StatusModels> liststatus = new List<StatusModels>();
            liststatus = status.getStatus();
            ViewBag.StatusListID = new SelectList(liststatus, "StatusID", "StatusName", LongTermLoanapp.StatusID);
            if (LongTermLoanapp != null)
            {
                LongTermLoanapp.StatusID = LongTermLoan.StatusID;
                LongTermLoanBLL.UpdateStatus(LongTermLoanapp);
                return RedirectToAction("Contract");
            }
            return View();
        }
        [Authorize(Roles = "8,9")]
        public ActionResult Contract()
        {
            StatusBLL status = new StatusBLL();
            List<StatusModels> liststatus = new List<StatusModels>();
            liststatus = status.getStatus();
            ViewBag.StatusID = liststatus;
            return View();
        }
        public JsonResult getContract()
        {

            LongTermLoanBLL LongTermLoanBLL = new LongTermLoanBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<LongTermLoanApplyModels> list = new List<LongTermLoanApplyModels>();
            list = LongTermLoanBLL.getcontract();
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        public JsonResult getcontractbystatus(int StatusID)
        {

            LongTermLoanBLL LongTermLoanBLL = new LongTermLoanBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<LongTermLoanApplyModels> list = new List<LongTermLoanApplyModels>();
            list = LongTermLoanBLL.getcontractbystatuscount(StatusID);
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        public ActionResult ThanksYou()
        {
            return View();
        }
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public JsonResult DeleteLongTermLoanApply(int LongTermLoanApplyID)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            LongTermLoanBLL bll = new LongTermLoanBLL();
            LongTermLoanApplyModels LongTermLoan = new LongTermLoanApplyModels();
            LongTermLoan = bll.getcontractbyid(LongTermLoanApplyID);
            if (LongTermLoan != null)
            {
                bll.DeleteLongTermLoanApply(LongTermLoan.LongTermLoanApplyID);
                var result = new { Status = "OK" };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            else
            {
                var result = new { Status = "Xóa thất bại" };
                return Json(result, JsonRequestBehavior.AllowGet);
            }

        }
        [HttpPost]
        public ActionResult SendEmail(string Name, string phone, string emailuser, int CardID)
        {
            try
            {
                string MailFromUserName = ConfigurationManager.AppSettings["MailFromUserName"].ToString();
                string MailFromPassword = ConfigurationManager.AppSettings["MailFromPassword"].ToString();
                string MailSMTP = ConfigurationManager.AppSettings["MailSMTP"].ToString();
                string MailCC = ConfigurationManager.AppSettings["MailCC"].ToString();
                string MailSendTo = ConfigurationManager.AppSettings["MailSendTo"].ToString();
                Models.EmailSend email = new Models.EmailSend();
                email.EmailSendTo = MailSendTo;
                email.Subject = "Khách hàng " + Name + " Apply Vay vốn trung, dài hạn ID: " + CardID + "";
                email.Body = "";
                email.Body += "<p>Hi, Admin</p>";
                email.Body += "<p>Khách hàng: " + Name + " Apply Vay vốn trung, dài hạn ID: " + CardID + "</p>";
                email.Body += "<p>Email: " + emailuser + "</p>";
                email.Body += "<p>SĐT: " + phone + "</p>";
                email.Body += "<p><a href='http://www.vnbanknet.com/LongTermLoan/ContractDetail/" + CardID + "'>Link chi tiết</a></p>";
                email.Body += "<p>Thanks</p>";
                MailMessage message = new MailMessage();
                message.Body = email.Body;
                message.IsBodyHtml = true;
                message.From = new System.Net.Mail.MailAddress(MailFromUserName);
                message.To.Add(email.EmailSendTo);
                message.CC.Add(MailCC);
                message.Subject = email.Subject;

                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(MailSMTP);// you need an smtp server address to send emails
                smtp.Credentials = new System.Net.NetworkCredential(MailFromUserName, MailFromPassword); //missing line from ur code
                smtp.Port = 587;  //missing line from your code
                smtp.EnableSsl = true;   //missing line from your code
                smtp.Send(message);
                int result = 1;
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                int result = -1;
                return Json(result, JsonRequestBehavior.AllowGet);
            }
        }
    }
}
